package com.example.ronnie_enzo.jumpgame;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private int screenWidth;//屏幕宽度
    private int screenHeigh;//屏幕高度
    private int stepSize;//单个跳板尺寸
    private float oneDp;//单位dp的像素
    private List<Boolean> jumpPath;//随机的游戏路径
    private List<Integer> jumpPathData;//随机的游戏路径
    private AbsoluteLayout layoutAbsDown;//路径布局
    private List<View> stepViewPath;//跳板路径
    private int lastX = 0;//上一次的x坐标
    private int currentIdx = 0;//当前步数;
    private Button btnDown;//跌落
    private Button btnNext;//跳
    private View view1, view2, view3, view4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        view1 = findViewById(R.id.view1);
        view2 = findViewById(R.id.view2);
        view3 = findViewById(R.id.view3);
        view4 = findViewById(R.id.view4);
        btnNext = findViewById(R.id.btnNext);
        btnDown = findViewById(R.id.btnDown);
        btnNext.setOnClickListener(this);
        btnDown.setOnClickListener(this);
        layoutAbsDown = findViewById(R.id.layoutAbsDown);
        initBaseData();
        initFirstView();
    }


    /**
     * 原始数据生成.
     * 屏幕尺寸.随机路径数据.路障大小
     */
    private void initBaseData() {
        WindowManager manager = this.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        screenWidth = outMetrics.widthPixels;
        screenHeigh = outMetrics.heightPixels;
        oneDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, this.getResources().getDisplayMetrics());
        stepSize = (int) (oneDp * 20);
        if (null == jumpPath) {
            jumpPath = new ArrayList<>();
        }
        if (null == jumpPathData) {
            jumpPathData = new ArrayList<>();
        }
        jumpPath.clear();
        if (null == stepViewPath) {
            stepViewPath = new ArrayList<>();
        }
        stepViewPath.clear();
        jumpPathData.clear();
        Loger.enzo("屏幕宽度: " + screenWidth + " @屏幕高度: " + screenHeigh + " @单位DP: " + oneDp);
        for (int i = 0; i < 100; i++) {
            int t = new Random().nextInt(100);
            jumpPathData.add(t);
            jumpPath.add(t % 2 == 0);
        }
    }

    /**
     * 游戏初始数据
     */
    private void initFirstView() {
        int startY = (int) (screenHeigh * 0.75);
        int currentX = screenWidth / 2;
        for (int i = 0; i < 7; i++) {
            currentX = jumpPath.get(currentIdx) ? (currentX + stepSize) : (currentX - stepSize);
            View v = new View(this);
            v.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(stepSize, stepSize, currentX, startY - i * stepSize);
            v.setLayoutParams(lp);
            currentIdx++;
            stepViewPath.add(v);
            layoutAbsDown.addView(v);
            Loger.enzo("i:" + i + " @currentIdx:" + currentIdx + " @starX:" + (screenWidth / 2) + " @startY:" + startY +
                    " @currentX坐标:" + currentX + " @Y坐标" + (startY - i * stepSize) + " 值:" + jumpPathData.get(i) + " 位置:" + jumpPath.get(i));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnNext:
                addNextOne();
                break;
            case R.id.btnDown:
                viewDownAnim();
                break;
        }
    }

    private int allViewDown = 0;
    private int allViewDownGap = 0;

    private void viewDownAnim() {
        allViewDownGap = (int) (oneDp * 35);
        Loger.enzo("动画集体跌落开始 :  allViewDown: " + allViewDown + "  allViewDownGap: " + allViewDownGap);

        ObjectAnimator oa1 = ObjectAnimator.ofFloat(view1, "translationY", allViewDown, allViewDown + allViewDownGap);
        ObjectAnimator oa2 = ObjectAnimator.ofFloat(view2, "translationY", allViewDown, allViewDown + allViewDownGap);
        ObjectAnimator oa3 = ObjectAnimator.ofFloat(view3, "translationY", allViewDown, allViewDown + allViewDownGap);
        ObjectAnimator oa4 = ObjectAnimator.ofFloat(view4, "translationY", allViewDown, allViewDown + allViewDownGap);
        AnimatorSet as = new AnimatorSet();
        as.setDuration(800L);
        as.playTogether(oa1, oa2, oa3, oa4);
        as.start();
        as.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                Loger.enzo("开始多个同事");
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                allViewDown = (allViewDown + allViewDownGap);
                Loger.enzo("动画集体跌落结束 :  allViewDown: " + allViewDown + "  allViewDownGap: " + allViewDownGap);

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });


    }

    private void addNextOne() {
        currentIdx++;
        View bottomView = stepViewPath.get(0);
        View topView = stepViewPath.get(6);
        bottomView.setBackgroundColor(getResources().getColor(R.color.color_EE4141));
        topView.setBackgroundColor(getResources().getColor(R.color.viewfinder_laser));
        AbsoluteLayout.LayoutParams oldLp = (AbsoluteLayout.LayoutParams) topView.getLayoutParams();
        ObjectAnimator bottomAnimator = ObjectAnimator.ofFloat(bottomView, "translationY", 0, screenHeigh * 0.3F);
        View v = new View(MainActivity.this);
        v.setBackgroundColor(getResources().getColor(R.color.tatlebar_bg));
//        AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) topView.getLayoutParams();// new AbsoluteLayout.LayoutParams(stepSize, stepSize, currentX, startY - i * stepSize);
        int x = jumpPath.get(currentIdx) ? (oldLp.x + stepSize) : (oldLp.x - stepSize);
        int y = (int) (screenHeigh * 0.75) - 6 * stepSize;
        AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(stepSize, stepSize, x, y);
        lp.x = x;
        lp.y = y;
        v.setLayoutParams(lp);
        stepViewPath.add(v);
        layoutAbsDown.addView(v);
        v.setTranslationY(((screenHeigh * 0.75F) - 6 * stepSize) - screenHeigh);

        ObjectAnimator topAnimator = ObjectAnimator.ofFloat(v, "translationY", 0, screenHeigh / 2);
        ObjectAnimator absMainAnimator = ObjectAnimator.ofFloat(layoutAbsDown, "translationY", stepSize);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(800L);
        set.play(bottomAnimator).with(topAnimator).with(absMainAnimator);
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Loger.enzo("播放结束");
                stepViewPath.remove(0);
                stepViewPath.size();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        set.start();
    }


}